const cloud = require('wx-server-sdk');

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
});

const db = cloud.database()
const User = db.collection('Users')

// 获取openId云函数入口函数
exports.main = async (event, context) => {
    const { OPENID } = cloud.getWXContext()
    const { nickname, avatar } = event

    result = await User.where({
        _openid: OPENID
    })
    .update({
        data: {
            nickname: nickname,
            avatar: avatar
        }
    })
    
    if (result.stats.hasOwnProperty('updated') && result.stats.updated == 1) {
        return {
            code: 1,
            msg: "修改成功"
        }
    } else {
        return {
            code: 0,
            msg: "未知错误"
        }
    }
};
